package com.common.util;

import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Optional;
import java.util.Random;

public class DateUtil {


    public static  String getNowWeekStr(){

        String pat = "yyyy-MM-dd HH:mm:ss EEEE";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getDateZeroStr(){

        String pat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        try {
            return format.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getNowDateStr(){

        String pat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static  String getYYYYMMDDStr(){

        String pat = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static ThreadLocal<SimpleDateFormat> datetimeFormat = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static ThreadLocal<SimpleDateFormat> timeFormat = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("HH:mm:ss");
        }
    };

    /**
     * 指定毫秒数表示日期的默认显示，具体格式：年-月-日
     *
     * @param millis 指定的毫秒数
     * @return 指定毫秒数表示日期按“年-月-日“格式显示
     */
    public static String formatDate(long millis) {
        return datetimeFormat.get().format(new Date(millis));
    }

    public static String formatTime(long millis) {
        return timeFormat.get().format(new Date(millis));
    }

    public static void main(String[] args) {

    }

}
